int num_temporaries = 0;
int num_labels = 0;

char * getNewTemp () {			//Returns a new temporary named _T<num>
	char * temp = malloc (5);
	strcpy (temp, "_T00");
	temp [2] = num_temporaries / 10 + '0';
	temp [3] = num_temporaries % 10 + '0';
	num_temporaries++;
	return temp;
}

char * getNewLabel () {			//Returns a new temporary named _T<num>
	char * temp = malloc (5);
	strcpy (temp, "_L00");
	temp [2] = num_labels / 10 + '0';
	temp [3] = num_labels % 10 + '0';
	num_labels++;
	return temp;
}
char *replicate(char *str){	//   
	char *temp;
	if(str!=NULL)
	{
	temp = (char *) malloc((sizeof (char))*strlen(str) + 1);
	strcpy (temp,str);
	}else
	{
	  temp = (char *) malloc((sizeof (char))*2);
	strcpy (temp,"");
	}
	//free(str);
	return temp;
}
char *append(char* str1, char* str2){	//   
	//printf("inside append\n");
	//printf("%d\n",strlen(str2));
	//printf("inside append\n");
	char* temp = (char *) malloc(sizeof(char) *(strlen(str1)+strlen(str2)) + 1);
	//printf("inside append1\n");
	strcpy (temp,str1);
	//printf("inside append2\n");
	strcat(temp,str2);
	//printf("inside append3\n");
	free(str1);
	//free(str2);
	return temp;
}

char *gen(char *instr, char *reg, int addr){
	char buf[100];
	sprintf(buf, "%d", addr);
	char *t = (char *)malloc(sizeof(char) * (strlen(instr) + strlen(reg) + strlen(buf) + 3));
	strcpy(t, instr);
	strcat(t, " ");
	strcat(t, reg);
	strcat(t, ",");
	strcat(t, buf);
	return t;
}

char * toAddr (int offset) {
  //printf("\n%d\n",offset);
	/*if (offset >= 100) {
		switch (offset) {
			case 100:
//				return "$t0";
				return "20($sp)";
			case 101:
//				return "$t1";
				return "16($sp)";
			case 102:
//				return "$t2";
				return "12($sp)";
			case 103:
//				return "$t3";
				return "8($sp)";
			default:
				return "$$$";
		}
	}
	if (offset > -10) {
		//printf ("OFFSET = %d\n", offset);
		return "@@@";
	}
	else */if (offset < 100) {
		char * temp = malloc (10);
		strcpy (temp, "---($sp)");
		//offset = - offset;
		temp [1] = (offset / 10) + '0';
		temp [2] = (offset % 10) + '0';
		return temp;
	}
	else {
		char * temp = malloc (12);
		strcpy (temp, "----($sp)");
		//offset = - offset;
		temp [1] = (offset / 100) + '0';
		temp [2] = ((offset % 100) / 10) + '0';
		temp [3] = (offset % 10) + '0';
		return temp;
	}
}